package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.ContractIdParam;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.heTongDownLoad.HeTongDownLoadParam;
import com.yunfei.api.v2.param.heTongSign.HeTongSignParam;
import com.yunfei.bo.TblobstorageBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.http.DownLoadContractHttpUtil;
import com.yunfei.utils.http.JsonParamHttpUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContBeneAcctDtl.bo.ElecContBeneAcctDtlBo;
import com.yunfei.yunhetong.elecContBeneAcctDtl.service.ElecContBeneAcctDtlService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;

/**
* @Description: 合同下载
* @Author: zhengyunfei
* @Date: 2019/8/27
* @time:10:52
*/
@Service
public class DownLoadContractService {
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private ElecContBeneAcctDtlService elecContBeneAcctDtlService;
    @Autowired
    private Environment env;
    /**
    * @Description: 合同下载
    * @Author: zhengyunfei
    * @Date: 2019/9/6
    * @time:14:24
    */
    public ResultModel downloadContract(HeTongDownLoadParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String token=getLongTokenService.getLongToken(new LongTokenParam());
            //获取合同存放服务器地址
            String savePath=env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String domain=env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN);
            //根据合同id，获取合同编号
            ElecContBeneAcctDtlBo queryParam=new ElecContBeneAcctDtlBo();
            queryParam.setContractId(param.getIdContent());
            ElecContBeneAcctDtlBo bo=elecContBeneAcctDtlService.findByBo(queryParam);
            String contractNum="";
            if(!StringUtil.isNullOrEmpty(bo)){
                 contractNum=bo.getContractNum();//合同编号，也是合同辩题
            }else{
                contractNum=param.getIdContent();
            }
            String today= DateUtil.getToDay("yyyyMMdd");
            savePath=savePath+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/";
            String fileName=contractNum+".pdf";
            String url= YunHeTongApiContant.DOWNLOAD_CONTRACT_API;//合同下载
            JSONObject jsonObject=JSONObject.fromObject(param);
            String jsonParam=jsonObject.toString();
            DownLoadContractHttpUtil.post(token,url,jsonParam,savePath,fileName);
            //返回文件访问文件
            String result=domain+"files/"+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/"+fileName;
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 作废协议下载
     * @Author: zhengyunfei
     * @Date: 2019/9/6
     * @time:14:24
     */
    public ResultModel downloadContractCancel(HeTongDownLoadParam param, ContractIdParam contractIdParam){
        ResultModel resultModel=new ResultModel();
        try{
            String token=getLongTokenService.getLongToken(new LongTokenParam());
            //获取合同存放服务器地址
            String savePath=env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String domain=env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN);
            String contractId=contractIdParam.getContractId();
            //根据合同id，获取合同编号
            ElecContBeneAcctDtlBo queryParam=new ElecContBeneAcctDtlBo();
            queryParam.setContractId(contractId);
            ElecContBeneAcctDtlBo bo=elecContBeneAcctDtlService.findByBo(queryParam);
            if(!StringUtil.isNullOrEmpty(bo)){
                String contractNum=bo.getContractNum();//合同编号，也是合同辩题
                String today= DateUtil.getToDay("yyyyMMdd");
                savePath=savePath+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/";
                String fileName=contractNum+"作废协议"+".pdf";
                String url= YunHeTongApiContant.DOWNLOAD_CONTRACT_API;//合同下载
                JSONObject jsonObject=JSONObject.fromObject(param);
                String jsonParam=jsonObject.toString();
                DownLoadContractHttpUtil.post(token,url,jsonParam,savePath,fileName);
                //返回文件访问文件
                String result=domain+"files/"+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/"+fileName;
                resultModel.setData(result);
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
   /**
   * @Description: 下载电子合同返回文件名和文件大小
   * @Author: zhengyunfei
   * @Date: 2019/8/29
   * @time:14:30
   */
    public TblobstorageEntity download(HeTongDownLoadParam param){
        TblobstorageEntity returnBo=new TblobstorageEntity();
        try{
            String token=getLongTokenService.getLongToken(new LongTokenParam());
            //获取合同存放服务器地址
            String savePath=env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String domain=env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN);
            //根据合同id，获取合同编号
            ElecContBeneAcctDtlBo queryParam=new ElecContBeneAcctDtlBo();
            queryParam.setContractId(param.getIdContent());
            ElecContBeneAcctDtlBo bo=elecContBeneAcctDtlService.findByBo(queryParam);
            if(!StringUtil.isNullOrEmpty(bo)){
                String contractNum=bo.getContractNum();//合同编号，也是合同辩题
                String today= DateUtil.getToDay("yyyyMMdd");
                savePath=savePath+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/";
                String fileName=contractNum+".pdf";
                String url= YunHeTongApiContant.DOWNLOAD_CONTRACT_API;//合同下载
                JSONObject jsonObject=JSONObject.fromObject(param);
                String jsonParam=jsonObject.toString();
                DownLoadContractHttpUtil.post(token,url,jsonParam,savePath,fileName);
                //返回文件访问文件
                String result=domain+"files/"+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/"+fileName;
                returnBo.setCFilename(fileName);
                //获取文件大小
                File myfile=new File(savePath+fileName);
                returnBo.setLFilesize(myfile.length()+"");
                returnBo.setCFilepath(result);
            }

        }catch (Exception e){

            e.printStackTrace();
        }
        return returnBo;
    }


}
